
#include "CoverData.h"
#include "../VPicture.h"

extern SingerCoversDeque g_CoverDeque;

CSingerCoverData::CSingerCoverData(void)
{
    m_bFlushing = false;
    m_CenterIndex = 0;
}

CSingerCoverData::~CSingerCoverData(void)
{
    FlushDeque();
}

// void CSingerCoverData::AddSingerCover( CSingerCover *pCover,GLuint nTextureID,int nHead )
// {
//  if (NULL == pCover)
//  {
//      return;
//  }
//
// //    pCover->SetTextrueID(nTextureID);
//
//  if (0 == nHead)
//  {
//      g_CoverDeque.push_front(pCover);
//  }
//  else
//  {
//      g_CoverDeque.push_back(pCover);
//  }
// }


void CSingerCoverData::GetDataFromDataBase(int nType)
{

}

void CSingerCoverData::SetDirection(int nDirection)
{
    m_Direction = nDirection;
}

void CSingerCoverData::FlushDeque()
{
    while (!g_CoverDeque.empty())
    {
        delete g_CoverDeque.front();
        g_CoverDeque.erase(g_CoverDeque.begin());
    }
}

void CSingerCoverData::AddCoverFront(singer_t* singer)
{
    CSingerCover* cover = new CSingerCover(singer);


    g_CoverDeque.push_front(cover);
    //  if (g_CoverDeque.size() < 2)
    //  {
    //      g_CoverDeque.push_front(new CSingerCover(pic));
    //      return;
    //  }
    //
    //  g_CoverDeque.push_front(NULL);
    //  for (size_t i = 1; i != g_CoverDeque.size()/2 - 1; ++i)
    //  {
    //      g_CoverDeque[i - 1] = g_CoverDeque[i];
    //  }
    //  g_CoverDeque[g_CoverDeque.size()/2 - 1] = new CSingerCover(pic);


    //  autoSetPosition();
}

void CSingerCoverData::AddCoverBack(singer_t* singer)
{
    CSingerCover* cover = new CSingerCover(singer);
    g_CoverDeque.push_back(cover);

    //autoSetPosition();
}

void CSingerCoverData::RemoveCoverFront()
{
    delete g_CoverDeque.front();

    g_CoverDeque.pop_front();

    autoSetPosition();
}

void CSingerCoverData::RemoveCoverBack()
{
    delete g_CoverDeque.back();

    g_CoverDeque.pop_back();

    autoSetPosition();
}

void CSingerCoverData::autoSetPosition()
{
    size_t iter;
    size_t size = g_CoverDeque.size();

    for (iter = 0; iter != size; ++iter)
    {
        setItemPosition(iter, size);
    }
}

void CSingerCoverData::setItemPosition(size_t pos, size_t size)
{
    int middle = size / 2;
    // g_CoverDeque[pos]->m_nID = pos;

    if (pos == middle)  // Item in middle
    {
        g_CoverDeque[pos]->m_v3Position.x = 0;
        g_CoverDeque[pos]->m_v3Position.y = 0;
        g_CoverDeque[pos]->m_v3Position.z = g_CenterZ;
        g_CoverDeque[pos]->m_fAngle = 0;
    }
    else if ((int)pos < middle)  // Item in left
    {
        g_CoverDeque[pos]->m_v3Position.x = -((middle - pos - 1) * g_IntervalCover + g_Interval) * .8;
        g_CoverDeque[pos]->m_v3Position.y = 0;
        g_CoverDeque[pos]->m_v3Position.z = 0;
        g_CoverDeque[pos]->m_fAngle = g_fAngle;
    }
    else                // Item in right
    {
        g_CoverDeque[pos]->m_v3Position.x = ((pos - middle - 1) * g_IntervalCover + g_Interval) * .8;;
        g_CoverDeque[pos]->m_v3Position.y = 0;
        g_CoverDeque[pos]->m_v3Position.z = 0;
        g_CoverDeque[pos]->m_fAngle = -g_fAngle;
    }
}
